package cc.sbootd2.sbootd2web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import cc.sbootd2.sbootd2web.pojo.News;
import cc.sbootd2.sbootd2web.pojo.NewsType;
import cc.sbootd2.sbootd2web.service.NewsService;
import cc.sbootd2.sbootd2web.service.NewsTypeService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/news")
public class NewsController {

    @Resource
    private NewsTypeService newsTypeService;
    @Resource
    private NewsService newsService;

    @RequestMapping("/add")
    public String add(Model model){
        List<NewsType> plist=newsTypeService.getAllNewsType();
        model.addAttribute("plist",plist);
        return "/news/add";
    }

    @RequestMapping( value = "/doadd",method = RequestMethod.POST)
    public String doAdd(Model model, News news, @RequestParam(value = "a_newsImg",required = false) MultipartFile
            file, HttpSession session){

        String filePath = session.getServletContext().getRealPath("upload");
        if (!file.isEmpty()) {
            String oldFileName=file.getOriginalFilename();
            //获取文件后坠名
            String nextfix=  oldFileName.split("\\.")[oldFileName.split("\\.").length-1]  ;
            if(nextfix.equals("jpg")||
                    nextfix.equals("png")||
                    nextfix.equals("gif")||
                    nextfix.equals("jpeg")){
//----------是图片文件则上传
                String newFileName=new Date().getTime()+""+ (int)(Math.random()*100000)+".jpg";
//           获得上传文件路径
//                   File tempFile = new File(filePath + "/" + file.getOriginalFilename());
                File tempFile = new File(filePath + "/" + newFileName);
                try {
                    if (!tempFile.exists()) {
                        tempFile.mkdirs();
                    }
                    //                上传附件
                    file.transferTo(tempFile);
                    //设置news对象的附件属性
//                       news.setNewsImg(file.getOriginalFilename());
                    news.setNewsImg(newFileName);
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }









        try {
            newsService.save(news);
        }catch (Exception e){
            e.printStackTrace();
        }

        return "redirect:/news/add";
    }




    @RequestMapping( value = "/doadd1",method = RequestMethod.POST)
    public String doAdd1(Model model, News news, HttpSession session){
        try {
            newsService.save(news);
        }catch (Exception e){
            e.printStackTrace();
        }

        return "redirect:/news/add";
    }




}
